package com.duoketech.service.weixin.order;

import com.duoketech.exception.ValidateException;
import com.duoketech.model.member.Member;
import com.duoketech.model.order.Order;
import com.duoketech.model.orderitem.OrderItem;
import com.duoketech.model.product.Product;
import com.duoketech.service.orderitem.OrderItemService;
import com.duoketech.utils.PageUtil;
import com.duoketech.utils.WxMsgTemplateUtils;
import com.duoketech.utils.search.EqualCondition;
import com.duoketech.utils.search.QueryConstructor;
import com.jfinal.plugin.activerecord.Page;

import java.util.Date;
import java.util.List;

public class WxOrderService {

    public static final WxOrderService ME = new WxOrderService();
    public Page<Order> queryOrdersOfMineBy(int pageNum, int pageSize, int memberId, int status){
        QueryConstructor queryConstructor = new QueryConstructor();
        queryConstructor.setCommand("from "+Order.dao.tableName());
        queryConstructor.and(new EqualCondition(Order.MEMBER_ID,memberId));
        if(status>0){
            queryConstructor.and(new EqualCondition(Order.STATUS,status));
        }

        String sql = queryConstructor.buildSQL();
        Page<Order> orders =  Order.dao.paginate(pageNum,pageSize,"select * ",sql,queryConstructor.getParams().toArray());
        for(Order order:orders.getList()){
            List<OrderItem> orderItems = queryOrderItemBy(order.getInt(Order.ID));
            order.put("items",orderItems);
            for(OrderItem o:orderItems){
                o.set(OrderItem.PRICE,o.price());
                o.set(OrderItem.IS_POST,o.isPost());
            }
        }
        return orders;
    }

    public List<OrderItem> queryOrderItemBy(int orderId){

        String sql="select t1.*,t2.name,t2.desc,t2.price,t2.cover from "+OrderItem.dao.tableName()+" t1,"+Product.dao.tableName()
                +" t2 where t1.product_id=t2.id and t1.order_id = ? and t2.status=? order by t1.id desc";
        return OrderItem.dao.find(sql,orderId,OrderItem.STATUS_NORMAL);
    }


    public void sendDelievryMsg(int memberId,int orderId) throws ValidateException {

        Order order = Order.dao.findById(orderId);
        if(order==null){
            throw new ValidateException("当前订单记录不存在");
        }

        int ownerId = order.getInt(Order.UNIT_ID);
        Member member = Member.dao.findById(ownerId);

        Member buyer = Member.dao.findById(memberId);

        WxMsgTemplateUtils.sendOrderReadyDeleveryMsg(member.getStr(Member.OPEN_ID),
                member.getStr(Member.NICK_NAME),order,buyer.getStr(Member.NICK_NAME));
    }


    public void confirmOrder(int memberId,int orderId) throws ValidateException {
        Order order = Order.dao.findById(orderId);
        if(order==null){
            throw new ValidateException("当前订单记录不存在");
        }
        if(order.getInt(Order.MEMBER_ID)!=memberId){
            throw new ValidateException("当前订单非本人购买，无法确认收货");
        }
        order.set(Order.STATUS,Order.STATUS_CONFIRM);
        order.update();
    }

    public Order queryOrderById(int orderId,int memberId){
        Order order = Order.dao.findById(orderId);
        List<OrderItem> orderItems = queryOrderItemBy(orderId);
        for (OrderItem each:orderItems) {
            each.set(OrderItem.PRICE,PageUtil.fengToYuan(each.getInt(OrderItem.PRICE)));
            each.set(OrderItem.IS_POST,each.isPost());
        }
        order.put("items",orderItems);
        return order;
    }


    public void delOrder(int orderId,int memberId) throws ValidateException {
        Order order = Order.dao.findById(orderId);
        if(order==null){
            throw new ValidateException("当前记录不存在");
        }
        if(order.getInt(Order.MEMBER_ID)!=memberId){
            throw new ValidateException("非本人订单，无操作权限");
        }

        if(order.getInt(Order.STATUS)>=Order.STATUS_PAID){
            throw new ValidateException("当前订单已经支付了，不允许被删除...");
        }
        order.set(Order.STATUS,Order.STATUS_DEL);
        order.set(Order.MODIFY_TIME,new Date());
        order.update();
    }

    public Order queryOrderById(int orderId){

        return Order.dao.findById(orderId);
    }
}
